package com.authine.cloudpivot.ext.utils.system;

import com.alibaba.cola.dto.RequestContext;
import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ServiceUrlConst;
import com.authine.cloudpivot.ext.dto.system.OrganizationDto;
import com.authine.cloudpivot.ext.dto.system.UserDto;
import com.authine.cloudpivot.ext.utils.HttpClients;
import com.authine.common.util.ObjectMapperUtils;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * @ClassName OrgUtils
 * @Description: 组织工具类，方便调用
 * @Author fj
 * @Date 2021/8/20
 * @Version V1.0
 **/
public class OrgUtils {
    /**
     * 根据组织id获取组织信息
     *
     * @param orgId 组织id
     * @return 组织信息
     */
    public static OrganizationDto getOrgByOrgId(String orgId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORG_BY_ID_THROUGH_JDBC_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("id", orgId);
        return HttpClients.execute(url, orgParam, OrganizationDto.class);
    }

    /**
     * 通过组织id判断当前组织是否存在
     *
     * @param orgId 组织id
     * @return true：存在 false：不存在
     */
    public static boolean exists(String orgId) {
        boolean result;
        try {
            // 组织id为空会抛出异常
            getOrgByOrgId(orgId);
            result = true;
        } catch (BizException e) {
            result = false;
        }
        return result;
    }

    /**
     * 根据组织名称获取组织信息
     *
     * @param orgName 组织名称
     * @return 组织信息
     */
    public static OrganizationDto getOrgByOrgName(String orgName) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORG_BY_ORG_NAME_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("name", orgName);
        return HttpClients.execute(url, orgParam, OrganizationDto.class);
    }

    /**
     * 根据组织id获取集团下所有组织
     * listGroupOrganization
     *
     * @param orgId 组织名称
     * @return 组织信息
     */
    public static List<OrganizationDto> getOrgListByOrgId(String orgId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORG_LIST_BY_ORG_ID_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("id", orgId);
        String execute = HttpClients.execute(url, orgParam);
        return ObjectMapperUtils.readValue(execute, OrganizationDto.class);
    }

    /**
     * 根据账户id获取对应的组织信息(可传多个账户id)
     *
     * @param accountIds 账户id
     * @return 组织信息列表
     */
    public static List<OrganizationDto> getOrgListByAccountIds(List<String> accountIds) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORG_LIST_BY_ACCOUNT_IDS_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("accountIds", accountIds);
        String execute = HttpClients.execute(url, orgParam);
        return ObjectMapperUtils.readValue(execute, OrganizationDto.class);
    }

    /**
     * 获取所有非指定的供应商组织列表
     *
     * @param ids 组织id
     * @return 组织信息列表
     */
    public static List<OrganizationDto> getVendorOrgListNotIds(List<String> ids) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_VENDOR_ORG_LIST_NOT_IDS_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("ids", ids);
        String execute = HttpClients.execute(url, orgParam);
        return ObjectMapperUtils.readValue(execute, OrganizationDto.class);
    }

    /**
     * 通过角色名称查询获取组织列表
     *
     * @param roleNames 角色名称
     * @return 组织信息列表
     */
    public static List<OrganizationDto> getOrgListByRoleNames(List<String> roleNames) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORG_LIST_BY_ROLE_NAMES_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("roleNames", roleNames);
        String execute = HttpClients.execute(url, orgParam);
        return ObjectMapperUtils.readValue(execute, OrganizationDto.class);
    }

    /**
     * 通过用户获取组织信息
     *
     * @return 组织信息
     */
    public static OrganizationDto getOrgByUserId() {
        return getOrgByUserId(RequestContext.getCorpLoginId());
    }

    /**
     * 通过用户获取组织信息
     *
     * @param userId 用户id
     * @return 组织信息
     */
    public static OrganizationDto getOrgByUserId(String userId) {
        UserDto user = UserUtils.getUserByUserId(userId);
        return getOrgByOrgId(user.getOrganization_id().get(0).getId());
    }

    /**
     * 客户查询被邀请的供应商组织
     *
     * @param customerId 客户id
     * @return 所有供应商id列表
     */
    public static List<String> listVendorByCustomerId(String customerId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.LIST_VENDOR_BY_CUSTOMER_ID_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("customerId", customerId);
        String execute = HttpClients.execute(url, orgParam);
        return ObjectMapperUtils.readValue(execute, String.class);
    }

    /**
     * 客户查询被邀请的供应商组织
     *
     * @param vendorId 客户id
     * @return 所有供应商id列表
     */
    public static List<String> listCustomerByVendorId(String vendorId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.LIST_CUSTOMER_BY_VENDOR_ID_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("vendorId", vendorId);
        String execute = HttpClients.execute(url, orgParam);
        return ObjectMapperUtils.readValue(execute, String.class);
    }

    /**
     * 根据多个组织id获取组织列表
     *
     * @param orgIds 组织id列表
     * @return 组织信息列表
     */
    public static List<OrganizationDto> getOrgListByOrgIds(List<String> orgIds) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORG_LIST_BY_ORG_IDS_URL);
        Map<String, Object> orgParam = Maps.newHashMap();
        orgParam.put("ids", orgIds);
        String execute = HttpClients.execute(url, orgParam);
        return ObjectMapperUtils.readValue(execute, OrganizationDto.class);
    }
}
